package util.android.cuestionario.fragment;

import android.view.View;
import android.view.ViewGroup;
import util.form.CuestionarioContext;
import util.form.respuesta.Formulario;
import util.form.respuesta.View0;
import util.form.respuesta.View1;
import util.form.respuesta.View2;

public class PageView 
extends Formulario 
{
	
	private final int position;
	private final ViewGroup rootView;
	private final PageViewListener pageViewListener;
	
	public PageView(CuestionarioContext context, int position, ViewGroup rootView, PageViewListener pageViewListener) {
		super(context);		
		this.position = position;
		this.rootView = rootView;
		this.pageViewListener = pageViewListener;
	}
	
	public View1 findView1ById(int viewID) {
		return (View1) rootView.findViewById(viewID);
	}
	
	public View2 findView2ById(int viewID) {
		return (View2) rootView.findViewById(viewID);
	}
		
	@SuppressWarnings("unchecked")
	public <V extends View> V findViewById(int viewID) {
		View v = rootView.findViewById(viewID);
		if(v instanceof View0) {
			View0 v0 = (View0)v;
			this.agregar(v0.getInput());
		}
		return (V)v;
	}	
	

	
	@SuppressWarnings("unchecked")
	public <V extends View1> V findViewById(int viewID, String columna) throws Exception {
		View1 v = (View1)rootView.findViewById(viewID);
		v.getInput().setNombre(columna);
		this.agregar(v.getInput());
		return (V)v;
	}
	
	@SuppressWarnings("unchecked")
	public <V extends View2> V findViewById(int viewID, String columna1, String columna2) throws Exception {
		View2 v = (View2)rootView.findViewById(viewID);
		v.getInput().getInput1().setNombre(columna1);
		v.getInput().getInput2().setNombre(columna2);
		this.agregar(v.getInput());
		return (V)v;
	}
	
			
	@Override
	public boolean equals(Object o) {		
		return o!=null && (o instanceof PageView) && position==((PageView)o).position;
	}

	protected PageViewListener getPageViewListener() {
		return pageViewListener;
	}


	public int getPosition() {
		return position;
	}

	public ViewGroup getRootView() {
		return rootView;
	}




	
}
